#include "acc_demuxer.h"

// 接收RTP包数据以及数据大小
void AACDemuxer::InputData(const uint8_t* data, size_t size) 
{
    // 1. 解析RTP头部
    struct RtpHeader* header = (struct RtpHeader*)data;
    int payload_type = header->payloadType;
    // 如果设置的payload_type和RTP包的payload_type不一致，则直接返回
    if(payload_type != payload_){
        return;
    }
    // 2. 提取有效的负载数据
    const uint8_t* payload = data + sizeof(struct RtpHeader); //去除负载头部的数据
    size_t payload_len = size - sizeof(struct RtpHeader); //负载的长度

    // 3. 解析RTP扩展头
    if(header->extension){
        // 计算拓展头的长度
        const uint8_t* extension_data = payload;
        size_t extension_length = 4 * (extension_data[2] << 8 | extension_data[3]);
        size_t payload_offset = 4 + extension_length;
        // 更新负载指针，同时更新负载长度
        payload = extension_data + payload_offset;
        payload_len -= payload_offset;
    }

    // 4. 回调函数
    if(call_back_){
        call_back_->OnAudioData(header->timestamp, payload, payload_len);
    }
    return;
}